﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using System.IO;

namespace Sys.SqlServer.Smo.Connection.Controls
{
    [ToolboxItem(true)]
    public partial class SqlConnectionBuilder : UserControl
    {
        public event EventHandler DatabaseChanged;

        public ConnectionString ConnectionString { get; private set; }

        public SqlConnectionBuilder()
        {
            InitializeComponent();

            ConnectionString = new ConnectionString();

            Load += new EventHandler(OnLoad);
        }

        private void OnLoad(object sender, EventArgs e)
        {            
            txtServer.Text = @".\SQLEXPRESS";
            txtUsername.Text = "sa";
            txtPassword.Text = "sa";
            txtDatabase.Text = "master";
        }

        private void OnDatabaseExpanded(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection c = new SqlConnection(ConnectionString.ToString()))
                {
                    Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(new Microsoft.SqlServer.Management.Common.ServerConnection(c));

                    txtDatabase.Items.Clear();

                    foreach (Microsoft.SqlServer.Management.Smo.Database db in server.Databases)
                        txtDatabase.Items.Add(db.Name);
                }
            }
            catch (Exception x)
            {

            }
        }

        private void OnCredentialsChanged(object sender, EventArgs e)
        {
            ConnectionString.Server = txtServer.Text;
            ConnectionString.Database = txtDatabase.Text;
            ConnectionString.Username = txtUsername.Text;
            ConnectionString.Password = txtPassword.Text;
            ConnectionString.Trusted = chkTrusted.Checked;

            txtUsername.Enabled = !ConnectionString.Trusted;
            txtPassword.Enabled = !ConnectionString.Trusted;

            txtConnectionString.Text = ConnectionString.ToString();
        }

        private void OnDatabaseKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                OnDatabaseChanged(sender, e);
        }

        private void OnDatabaseChanged(object sender, EventArgs e)
        {
            if (DatabaseChanged != null)
                DatabaseChanged(this, e);
        }

        private void OnTesting(object sender, EventArgs e)
        {
            try
            {
                if (ConnectionString.TestConnection())
                    MessageBox.Show("Test connection successfull!");
                else
                    MessageBox.Show("Test connection failed!");
            }
            catch
            {
                
            }
        }

        private void OnDataSave(object sender, EventArgs e)
        {
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "XML Files|*.xml";
            d.AddExtension = true;
            d.CheckFileExists = false;
            d.CheckPathExists = false;

            if (d.ShowDialog() == DialogResult.OK)
            {
                Invalidate();
                ConnectionString.Save(new FileInfo(d.FileName));
            }
        }

        private void OnDataLoad(object sender, EventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();
            d.Filter = "XML Files|*.xml";
            d.AddExtension = true;
            d.CheckFileExists = true;
            d.CheckPathExists = true;
            d.Multiselect = false;

            if (d.ShowDialog() == DialogResult.OK)
            {
                Invalidate();

                ConnectionString.Load(new FileInfo(d.FileName));

                txtServer.Items.Add(ConnectionString.Server);
                txtDatabase.Items.Add(ConnectionString.Database);
                txtUsername.Items.Add(ConnectionString.Username);
                txtPassword.Items.Add(ConnectionString.Password);

                txtServer.SelectedIndex = 0;
                txtDatabase.SelectedIndex = 0;
                txtUsername.SelectedIndex = 0;
                txtPassword.SelectedIndex = 0;

                chkTrusted.Checked = ConnectionString.Trusted;
            }
        }
    }
}